package yxy.game.pm2.chat.command;

import yxy.apple.nio.server.Server;
import yxy.apple.util.StringUtillity;
import yxy.apple.util.UnicodeUtility;
import yxy.cherry.data.dfa.Sensitive;
import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.ChatChannelType;
import yxy.game.pm2.bean.cluster.ClusterBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.chat.module.Banned;
import yxy.game.pm2.chat.module.BaseChatChannelModule;
import yxy.game.pm2.chat.module.ChatHandle;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ChatMessage;
import yxy.game.pm2.message.ChatMessage.ChatChannel;
import yxy.game.pm2.module.CurrencyHandle;

/** 聊天 聊天发送 */
@MessageType(proto = Command.ChatSend, classtype = ChatMessage.ChatSendRequest.class)
public class CMD_ChatSend extends Command<ChatMessage.ChatSendRequest> {

	@Override
	public void execute(MessageSource messageSource, ChatMessage.ChatSendRequest request) {
		String playerCode = messageSource.getTag();
		ChatChannel channel = request.getChannel();
		String text = request.getText();

		ChatChannelType channelType = ChatChannelType.forNumber(channel.getNumber());

		ChatMessage.ChatSendResponse.Builder response = ChatMessage.ChatSendResponse.newBuilder();

		if (Banned.getInstance().isBanned(playerCode)) {
			send(messageSource, response, Result.ChatBanned);// 禁言中
			return;
		}

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		ClusterBean clusterBean = ClusterBean.get(areaBean.getClusterId());
		BaseChatChannelModule chatChannelModule = ChatHandle.getChannel(
				clusterBean.getClusterId(),
				areaBean.getGroupId(),
				areaBean.getAreaId(),
				playerBean.getCode(),
				channelType
		);
		if (chatChannelModule == null) {
			send(messageSource, response, Result.ParamError);// 频道关闭中
			return;
		}
		// 判断发送间隔
		boolean intervalTooShort = chatChannelModule.checkInterval(playerCode);
		if (intervalTooShort) {
			send(messageSource, response, Result.ChatSendFast);// 说话太快了
			return;
		}

		if (channelType.equals(ChatChannelType.CHAT_WORLD)) {
//			PlayerBean playerBean = PlayerBean.get(playerCode);
			int levelLimit = fomula.跨服聊天等级限制.intValue();
			int viplevelLimit = fomula.跨服聊天VIP等级限制.intValue();
			if (playerBean.getLevel() < levelLimit && playerBean.getViplevel() < viplevelLimit) {
				send(messageSource, response, Result.ChatWorldChannelLevelLow);// 玩家等级不够
				return;
			}

			/*int costNum = fomula.跨服聊天花费元宝数.intValue();
			Currency cost = Currency.create(CurrencyType.CT_元宝, 0, costNum);
			try {
				new CurrencyHandle().cost(playerCode, CCReason.聊天, cost);
			} catch (CurrencyNotEnoughException e) {
				send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
				return;
			}*/
		}

		text = StringUtillity.toDBC(text).trim();
		if (text == null || text.length() <= 0) {
			send(messageSource, response, Result.ChatTextNotStandard);// 文字内容不太合适
			return;
		}
		text = UnicodeUtility.trimUnicode(text, UnicodeUtility.UnicodeSet_Chat);
		if (text.length() > 20) {
			send(messageSource, response, Result.ChatTextLengthTooLong);// 文字字数过多
			return;
		}
		if (Sensitive.isSensitive(text)) {
			send(messageSource, response, Result.ChatTextNotStandard);// 敏感 文字内容不太合适
			return;
		}

		ChatHandle.send(playerCode, channelType, text);
	}

	private void send(MessageSource messageSource, ChatMessage.ChatSendResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}